package com.zbkj.crmeb.merchant.api.controller;

import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.merchant.api.request.ExchangeProductRequest;
import com.zbkj.crmeb.merchant.api.response.UserIntegralStoreResponse;
import com.zbkj.crmeb.store.dto.ChannelIntegralConfigDTO;
import com.zbkj.crmeb.store.dto.ChannelIntegralProductConfigDTO;
import com.zbkj.crmeb.store.model.OrderInfoDelivery;
import com.zbkj.crmeb.store.request.ChannelIntegralExchangeRequest;
import com.zbkj.crmeb.store.request.OrderInfoDeliveryRequest;
import com.zbkj.crmeb.store.response.ChannelIntegralExchangeResponse;
import com.zbkj.crmeb.store.service.ChannelIntegralConfigService;
import com.zbkj.crmeb.store.service.ChannelIntegralExchangeService;
import com.zbkj.crmeb.store.service.ChannelIntegralProductConfigService;
import com.zbkj.crmeb.user.response.UserIntegralSignResponse;
import com.zbkj.crmeb.user.service.UserIntegralSignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * 功能描述: 小程序积分商城
 * @Author: wangzh
 * @Date: 2023/3/1 9:40
 */
@RestController
@RequestMapping("api/front/mer/integral/store")
@Api(tags = "积分商城")
public class FontIntegralStoreController extends ApiBaseController{

    @Autowired
    private ChannelIntegralProductConfigService channelIntegralProductConfigService;
    @Autowired
    private ChannelIntegralConfigService channelIntegralConfigService;
    @Autowired
    private UserIntegralSignService userIntegralSignService;
    @Autowired
    private ChannelIntegralExchangeService channelIntegralExchangeService;

    @GetMapping
    @ApiOperation("获取兑换专区列表")
    public CommonResult<CommonPage<ChannelIntegralProductConfigDTO>> getList(HttpServletRequest request) {
        ChannelIntegralProductConfigDTO dto = new ChannelIntegralProductConfigDTO();
        dto.setMerId(getMerId(request));
        PageParamRequest pageParamRequest = new PageParamRequest();
        pageParamRequest.setPage(1);
        pageParamRequest.setLimit(9999);
        return CommonResult.success(CommonPage.restPage(channelIntegralProductConfigService.getIntegralProductConfig(dto, pageParamRequest, true)));
    }

    @GetMapping("/sign")
    @ApiOperation("签到")
    public CommonResult<UserIntegralSignResponse> sign() throws Exception {
        return CommonResult.success(userIntegralSignService.sign());
    }

    @GetMapping("/info")
    @ApiOperation("获取商城信息")
    public CommonResult<UserIntegralStoreResponse> getSignStatus(HttpServletRequest request) {
        UserIntegralStoreResponse response = new UserIntegralStoreResponse();
        response.setStatus(userIntegralSignService.getSignStatus());
        response.setChannelIntegralConfigDTO(channelIntegralConfigService.getIntegralConfig(getMerId(request)));
        response.setUserIntegralSignResponse(userIntegralSignService.getSignRecord());
        return CommonResult.success(response);
    }

    @GetMapping("/status")
    @ApiOperation("获取积分商城状态")
    public CommonResult<Boolean> getIntegralStoreStatus(HttpServletRequest request) {
        return CommonResult.success(channelIntegralConfigService.getIntegralStoreStatus(getMerId(request)));
    }

    @PostMapping("/exchange")
    @ApiOperation("兑换商品")
    public CommonResult<Boolean> exchangeProduct(@RequestBody ExchangeProductRequest request) {
        return CommonResult.success(channelIntegralProductConfigService.exchangeProduct(request.getId(), request.getAttrValueId(), request.getAddressId()));
    }

    @GetMapping("/exchange")
    @ApiOperation("兑换列表")
    public CommonResult<CommonPage<ChannelIntegralExchangeResponse>> getExchangeList(ChannelIntegralExchangeRequest request, HttpServletRequest servletRequest) {
        return CommonResult.success(CommonPage.restPage(channelIntegralExchangeService.getList(request, getMerId(servletRequest), true)));
    }

    @GetMapping("/delivery")
    @ApiOperation("获取兑换商品发货记录")
    public CommonResult<List<OrderInfoDelivery>> getDeliveryList(@Validated OrderInfoDeliveryRequest request) {
        return CommonResult.success(channelIntegralExchangeService.getDeliveryList(request));
    }
}
